package com.jrx.anytxn.transaction.job.paph.step;

import com.jrx.anytxn.common.uitl.DateUtils;
import com.jrx.anytxn.param.constant.Constant;
import com.jrx.anytxn.param.entity.PrOrganizationTable;
import com.jrx.anytxn.param.entity.PrProductTable;
import com.jrx.anytxn.param.service.product.IProductTableService;
import com.jrx.anytxn.param.service.system.IOrganizationTableService;
import com.jrx.anytxn.transaction.bean.PaphFileBean;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import com.jrx.anytxn.transaction.entity.AmLoanMainInfo;
import com.jrx.anytxn.transaction.entity.TlDisbursementLog;
import com.jrx.anytxn.transaction.mapper.ext.ExtAmLoanMainInfoMapper;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Date;


@Service
public class TraAcctDetailprocesser implements ItemProcessor<TlDisbursementLog, PaphFileBean> {
    @Autowired
    private ExtAmLoanMainInfoMapper extAmLoanMainInfoMapper;
    @Autowired
    private IOrganizationTableService organizationTableService;
    @Autowired
    private IProductTableService productTableService;

    @Override
    public PaphFileBean process(TlDisbursementLog log) throws Exception {
        String loanId = log.getLoanId();
        PrOrganizationTable org = organizationTableService.findByOrgIdForBatch(Constant.DEFAULT_ORG_NUMBER);
        AmLoanMainInfo amLoanMainInfo = extAmLoanMainInfoMapper.selectByLoanIdAndTenantId(loanId, org.getTenantId());
        if(null == amLoanMainInfo){
            return null;
        }
        String businessDate = DateUtils.format(log.getCompleteTime(), "yyyyMMddHHmmss");
        String currentDate = DateUtils.format(new Date(), "yyyyMMddHHmmss");
        DecimalFormat df = new DecimalFormat("0.0000000");
        BigDecimal interestRate = amLoanMainInfo.getInterestRate();
        BigDecimal transInterestRate = interestRate.movePointRight(2);
        String businessRate = df.format(transInterestRate);
        PaphFileBean paphFileBean = new PaphFileBean();


        StringBuffer sb = new StringBuffer();
        sb.append(TransBizConstant.FIRST_WORD);
        sb.append(TransBizConstant.FILE_SEPARATOR);
        //申请号
        sb.append(amLoanMainInfo.getContractNumber());
        sb.append(TransBizConstant.FILE_SEPARATOR);
        //合作渠道
        sb.append(TransBizConstant.INSURE_COMPANY);
        sb.append(TransBizConstant.FILE_SEPARATOR);
        //借据号
        sb.append(amLoanMainInfo.getLoanId());
        sb.append(TransBizConstant.FILE_SEPARATOR);
        //客户姓名
        sb.append(log.getPayeeName());
        sb.append(TransBizConstant.FILE_SEPARATOR);
        //放款时间
        //放款金额
        //执行利率
        //转账金额
        //放款结果
        //转账标识
        //异常代码
        //异常原因
        if (TransBizConstant.PAYMENT_LOG_SUM_STATUS_S.equals(log.getSumStatus())) {
            sb.append(businessDate);
            sb.append(TransBizConstant.FILE_SEPARATOR);
            sb.append(log.getAmount());
            sb.append(TransBizConstant.FILE_SEPARATOR);
            sb.append(businessRate);
            sb.append(TransBizConstant.FILE_SEPARATOR);
            sb.append(log.getAmount());
            sb.append(TransBizConstant.FILE_SEPARATOR);
            sb.append(TransBizConstant.LOAN_SUCCEED);
            sb.append(TransBizConstant.FILE_SEPARATOR);
            sb.append(TransBizConstant.TRANSFER_SUCCEED);
            sb.append(TransBizConstant.FILE_SEPARATOR);
            sb.append(TransBizConstant.FILE_SEPARATOR);
            sb.append(TransBizConstant.SUCCESSD_STATUS);
            sb.append(TransBizConstant.FILE_SEPARATOR);
        } else {
            sb.append(currentDate);
            sb.append(TransBizConstant.FILE_SEPARATOR);
            sb.append(TransBizConstant.FAIL_LOANAMT);
            sb.append(TransBizConstant.FILE_SEPARATOR);
            sb.append(TransBizConstant.FAIL_BUSINESS_RATE);
            sb.append(TransBizConstant.FILE_SEPARATOR);
            sb.append(TransBizConstant.FAIL_LOANAMT);
            sb.append(TransBizConstant.FILE_SEPARATOR);
            sb.append(TransBizConstant.LOAN_FAILED);
            sb.append(TransBizConstant.FILE_SEPARATOR);
            sb.append(TransBizConstant.TRANSFER_FAILED);
            sb.append(TransBizConstant.FILE_SEPARATOR);
            sb.append(TransBizConstant.FAIL_SUM_STATUS);
            sb.append(TransBizConstant.FILE_SEPARATOR);
            sb.append(log.getReturnMessage());
            sb.append(TransBizConstant.FILE_SEPARATOR);
        }


        sb.append(amLoanMainInfo.getSerialNumber());
        paphFileBean.setFileString(sb.toString());
        paphFileBean.setProductType(amLoanMainInfo.getLoanType());
        return paphFileBean;

    }
}